package com.zcf.controller;
import com.zcf.common.json.Body;
import com.zcf.gameController.gameUtils.UserThread;
import com.zcf.gameController.gameUtils.XtThread;
import com.zcf.pojo.HigherRoom;
import com.zcf.service.GameRoomServiceImpl;
import com.zcf.vo.out.LayuiJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author jj
 * @date 2019/11/9 16:53
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@Controller
@RequestMapping("/room")
public class GameRoomController {
    @Autowired
    private GameRoomServiceImpl gameRoomService;

    /**
     * @description :心跳和检测用户是否存在
     * @date : 2019/11/26 9:28
     * @author : jj
     */
    static {
        XtThread xt=new XtThread();
        xt.start();
        UserThread userThread=new UserThread();
        userThread.start();
    }

    /**
    * @description :添加房间
    * @param higherRoom
    * @date : 2019/11/11 19:53
    * @author : jj
    */
    @PostMapping("/add")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public Body  addRoom(@RequestBody HigherRoom higherRoom) {
        return gameRoomService.addRoom(higherRoom);
    }
    /**
    * @description :查询所有房间
    * @param pageNum
    * @param pageSize
    * @date : 2019/11/11 19:54
    * @author : jj
    */
    @RequestMapping("/listrooms")
    @ResponseBody
    public LayuiJson listAll(@RequestParam(value = "pageNum") int pageNum, @RequestParam(value = "pageSize") int pageSize) {
        return gameRoomService.listRooms(pageNum,pageSize);
    }

    /**
     * 更新房间
     * @param higherRoom
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public Body  updateRoom(HigherRoom higherRoom) {
        return gameRoomService.updateRoom(higherRoom);
    }
    /**
     * @description :分类查询开始的房间
     * @param gameType
     * @param roomGrade
     * @date : 2019/11/11 19:54
     * @author : jj
     */
    @PostMapping("/listClassifyRooms")
    @ResponseBody
    public Body listGameRooms(Integer gameType,Integer roomGrade) {
        return gameRoomService.listClassifyRooms(gameType,roomGrade);
    }
    /**
     * @description :开启房间
     * @param id
     * @param state
     * @date : 2019/11/11 19:54
     * @author : jj
     */
    @PostMapping("/open")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public Body openRoom( Long id, Integer state){ return gameRoomService.openRoom(id,state); }

    /**
     * 关闭房间
     * @param id
     * @param state
     * @return
     */
    @PostMapping("/close")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public Body closeRoom( Long id, Integer state){ return gameRoomService.closeRoom(id,state); }
    /**
    * @description :根据房间号查找该房间号属于那个websocket
    * @param roomNum
    * @date : 2019/11/11 19:55
    * @author : jj
    */
    @PostMapping("/bitroom")
    @ResponseBody
    public Body bitRoom(String roomNum){
        return gameRoomService.bitRoom(roomNum);
    }

}
